package com.mybatis.model;

import java.io.Reader;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.mybatis.inter.IUserOperation;


public class Test {
    private static SqlSessionFactory sqlSessionFactory;
    private static Reader reader;

    static{
        try{
            reader    = Resources.getResourceAsReader("Configuration.xml");
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    public static SqlSessionFactory getSession(){
        return sqlSessionFactory;
    }

    public void getUserList(String userName){
        SqlSession session = sqlSessionFactory.openSession();
        try {
            IUserOperation userOperation=session.getMapper(IUserOperation.class);
            List<User> users = userOperation.selectUsers(userName);
            for(User user:users){
                System.out.println("id:"+user.getId()+"  name:"+user.getName()
                        +"  sex:"+user.getSex()+"  login:"+user.getLogin()+"  password:"+user.getPassword()
                        +"  phone:"+user.getPhone()+"  department:"+user.getDepartment()
                        +"  position:"+user.getPosition()+"  role:"+user.getRole());
            }

        } finally {
            session.close();
        }
    }

    /**
     * 测试增加,增加后，必须提交事务，否则不会写入到数据库.
     */
    public void addUser(String name,String sex,String login,String password,String phone,String department,String position,String role){
        User user=new User();
        user.setName(name);
        user.setSex(sex);
        user.setLogin(login);
        user.setPassword(password);
        user.setPhone(phone);
        user.setDepartment(department);
        user.setPosition(position);
        user.setRole(role);
        SqlSession session = sqlSessionFactory.openSession();
        try {
            IUserOperation userOperation=session.getMapper(IUserOperation.class);
            userOperation.addUser(user);
            session.commit();
            System.out.println("当前增加的用户 id为:"+user.getId());
        } finally {
            session.close();
        }
    }

    //更新
    public void updateUser(){
        //先得到用户,然后修改，提交。
        SqlSession session = sqlSessionFactory.openSession();
        try {
            IUserOperation userOperation=session.getMapper(IUserOperation.class);
            User user = userOperation.selectUserByID(2);
            //user.setUserAddress("原来是魔都的浦东创新园区");
            userOperation.updateUser(user);
            session.commit();

        } finally {
            session.close();
        }
    }

    /**
     * 删除数据，删除一定要 commit.
     * @param id
     */
    public void deleteUser(int id){
        SqlSession session = sqlSessionFactory.openSession();
        try {
            IUserOperation userOperation=session.getMapper(IUserOperation.class);
            userOperation.deleteUser(id);
            session.commit();
        } finally {
            session.close();
        }
    }

    //SqlSession实例查询
//    public static void main(String[] args) {
//        SqlSession session = sqlSessionFactory.openSession();
//        try {
//        User user = (User) session.selectOne("com.mybatis.models.UserMapper.selectUserByID", 1);
//        //System.out.println(user.getUserAddress());
//        System.out.println(user.getName());
//        } finally {
//        session.close();
//        }
//    }

    //IUserOperation接口查询
//    public static void main(String[] args) {
//        SqlSession session = sqlSessionFactory.openSession();
//        try {
//            IUserOperation userOperation=session.getMapper(IUserOperation.class);
//            User user = userOperation.selectUserByID(1);
//            //System.out.println(user.getUserAddress());
//            System.out.println(user.getName());
//        } finally {
//            session.close();
//        }
//    }

    //查询列表
    public static void main(String[] args) {
        Test testUser=new Test();
        testUser.getUserList("summer");
        //testUser.getUserList("summer");
        //testUser.addUser();
        //testUser.addUser("cjy","m","x0x0x01","123","18188023256","java","职员","a");
    }
}
